﻿module game.battle.projectiles.baseprojectiles;

import std.range: isForwardRange;

import gamelib.types;
import gamelib.graphics.renderer2d.renderer;

import game.units;
import game.transformmapper;

struct SimpleColorProjectile(alias color, int MaxTicks = 50)
{
    pos_t pos;
    pos_t speed;
    int damage = 1;
    int ticksRemaining = MaxTicks;

    void kill() pure nothrow
    {
        ticksRemaining = 0;
    }

    void update() pure nothrow
    {
        --ticksRemaining;
        pos += speed;
    }

    @property bool isAlive() const pure nothrow { return ticksRemaining > 0; }
    
    struct ProjectileRenderer
    {
        private Rect[] mRects;
        
        void draw(T)(Renderer ren, in TransformMapper mapper, T range) if(isForwardRange!T)
        {
            if(range.empty)
            {
                return;
            }
            
            if(range.length > mRects.length)
            {
                mRects.length = calcCacheSize(range.length);
            }
            
            int i = 0;
            foreach(p;range)
            {
                auto pt = mapper.map(p.pos);
                mRects[i] = Rect(pt.x - 1, pt.y - 1, 3, 3);
                ++i;
            }
            ren.drawColor = color;
            ren.drawFilledRects(mRects[0..i]);
        }
    }
}